package com.ls.fw.data.bean;

import java.util.ArrayList;
import java.util.List;


/**
 * 分页实体
 * @author 李昇
 * 2014年4月19日 上午9:52:16
 */
public class PageBean extends SimplePage {
	
	private static final long serialVersionUID = 1408655727648117820L;
	
	@SuppressWarnings("rawtypes")
	private List<?> list = new ArrayList(1); // 要返回的某一页的记录列表
	 
	private boolean firstPage = true; // 是否为第一页
	private boolean lastPage = false; // 是否为最后一页
	private boolean hasPreviousPage = false; // 是否有前一页
	private boolean hasNextPage = false; // 是否有下一页
	
	/**
	 * 
	 * @param page 第几页
	 * @param pageSize 页面显示记录数
	 * @param allRow 总记录数
	 * @param list 某一页的记录列表
	 * @return
	 */
	public static <T> PageBean getInstance(int page, int pageSize, long allRow,
			List<T> list) {
		int totalPage = 0;
		if(pageSize<1){
			totalPage = PageBean.countTotalPage((int) allRow, allRow); // 总页数
		}else{
			totalPage = PageBean.countTotalPage(pageSize, allRow); // 总页数
		} 
		final int currentPage = PageBean.countCurrentPage(page);// 当前页
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		return pageBean;
	}

	/**
	 * 
	 * @param page
	 *            //第几页
	 * @param pageSize
	 *            //每一页记录数
	 * @param list
	 * @return
	 */
	public static <T> PageBean getInstance(int page, int pageSize, List<T> list) {
		return PageBean.getInstance(page, pageSize, 0, list);
	}
	
	private void  init(){
		this.firstPage = (page == 1);// 如是当前页是第1页
		this.lastPage = (page == totalPage); // 如果当前页是最后一页
		this.hasPreviousPage = (page != 1); // 只要当前页不是第1页
		this.hasNextPage = (page != totalPage); // 只要当前页不是最后1页
	}
	
	public PageBean() { 
	}

	

    /**
     * 包装Page对象
     *
     * @param list          page结果
     * @param navigatePages 页码数量
     */
    public <T> PageBean(List<T> list) {
         this.setList(list);
    }
   
	
	/**
	 * KNOW 计算总页数,静态方法,供外部直接通过类名调用
	 * 
	 * @param pageSize
	 *            每页记录数
	 * @param allRow
	 *            总记录数
	 * @return 总页数
	 */
	public static int countTotalPage(final int pageSize, final long allRow) {
		long totalPage = 0;
		if(allRow>0){
			 totalPage = allRow % pageSize == 0 ? allRow / pageSize : allRow
					/ pageSize + 1;
		} 
		return (int) totalPage;
	}

	/**
	 * KNOW 计算当前页开始记录
	 * 
	 * @param pageSize
	 *            每页记录数
	 * @param currentPage
	 *            当前第几页
	 * @return 当前页开始记录号
	 */
	public static int countOffset(final int pageSize, final int currentPage) {
		final int offset = pageSize * (currentPage - 1);
		return offset;
	}

	/**
	 * KNOW 计算当前页,若为0或者请求的URL中没有" page=",则用1代替
	 * 
	 * @param page
	 *            传入的参数(可能为空,即0,则返回1)
	 * @return 当前页 当前页是第几页
	 */
	public static int countCurrentPage(int page) {
		final int curPage = (page <= 0 ? 1 : page);
		return curPage;
	}

	public List<?> getList() {
		return list;
	}

	public void setList(List<?> list) {
		this.list = list;
		if (list instanceof PageList) {
        	PageList page = (PageList) list;
            this.page = page.getPage();
            this.pageSize = page.getPageSize();

            this.allRow = page.getAllRow();
            this.totalPage = page.getTotalPage();
            this.list = page;
            int size = page.size();
            //由于结果是>startRow的，所以实际的需要+1
            if (size == 0) {
                this.startRow = 0;
                this.endRow = 0;
            } else {
                this.startRow = page.getStartRow() + 1;
                //计算实际的endRow（最后一页的时候特殊）
                this.endRow = this.startRow - 1 + size;
            }
        }
	}   

	public void setFirstPage(boolean firstPage) {
		this.firstPage = firstPage;
	}

	public void setLastPage(boolean lastPage) {
		this.lastPage = lastPage;
	}

	public void setHasPreviousPage(boolean hasPreviousPage) {
		this.hasPreviousPage = hasPreviousPage;
	}
	

	public boolean isFirstPage() {
		this.firstPage = (page == 1);// 如是当前页是第1页
		return firstPage;
	}

	
	public boolean isLastPage() {
		this.lastPage = (page == totalPage); // 如果当前页是最后一页
		return this.lastPage;
	}

	public boolean isHasPreviousPage() {
		this.hasPreviousPage = (page != 1); // 只要当前页不是第1页
		return this.hasPreviousPage;
	}

	public boolean isHasNextPage() {
		this.hasNextPage = (page != totalPage); // 只要当前页不是最后1页
		return this.hasNextPage;
	}

	@Override
	public String toString() {
		return "PageBean [offset=" + this.offset + ", page=" + this.page
				+ ", pageSize=" + this.pageSize + ", totalPage="
				+ this.totalPage + ", allRow=" + this.allRow + ", list="
				+ this.list + ", firstPage=" + this.firstPage + ", lastPage="
				+ this.lastPage + ", hasPreviousPage=" + this.hasPreviousPage
				+ ", hasNextPage=" + this.hasNextPage + "]";
	}
	
	
}